import { _decorator, Component, Node, input, Input, EventKeyboard, tween, EventMouse, math, Vec3, ITriggerEvent, CylinderCollider, ICollisionEvent } from "cc";
const { ccclass, property } = _decorator;
import { chatBox } from "./chatBox";
/**
 * Predefined variables
 * Name = player
 * DateTime = Sat Feb 26 2022 21:24:38 GMT+0800 (中国标准时间)
 * Author = zxq6635624
 * FileBasename = player.ts
 * FileBasenameNoExtension = player
 * URL = db://assets/script/player.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass("player")
export class player extends Component {
  // [1]
  // dummy = '';

  // [2]
  // @property
  // serializableDummy = 0;
  static keydown_w = false;
  static keydown_s = false;
  static keydown_a = false;
  static keydown_d = false;

  mouse_x = null;

  start() {
    
    // [3]
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    input.on(Input.EventType.KEY_UP, this.onKeyUp, this);

    input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
    input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);

    let Collider = this.node.getComponent(CylinderCollider);
    Collider.on("onCollisionEnter", this.onCollision, this);
  }
  //碰撞检测
  private onCollision(event: ICollisionEvent) {
    console.log(event.type, event);
  }

  onMouseDown(event: EventMouse) {
    this.mouse_x = event.getLocationX();
    input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
  }
  onMouseMove(event: EventMouse) {
    let new_x = event.getLocationX();
    let rotation_y = this.node.eulerAngles.y;
    rotation_y += (new_x - this.mouse_x) * -1;
    this.node.setRotationFromEuler(0, rotation_y, 0);
    this.mouse_x = new_x;
    this.posUpdateOnline_mouseMove()
  }
  onMouseUp(event: EventMouse) {
    input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
  }

  posUpdateOnline_keyDown(keycode:number) {
    
    let msg = {
      code: 300,
      message: {
        token: localStorage.getItem("token"),
        data:{
          keycode:keycode,
          pos:this.node.getPosition()
        }
      }
    }
    if (chatBox.ws) {
      chatBox.ws.send(JSON.stringify(msg));
    }
    
  }
  posUpdateOnline_keyUp(keycode:number) {
    let msg = {
      code: 301,
      message: {
        token: localStorage.getItem("token"),
        data:{
          keycode:keycode,
          pos:this.node.getPosition()
        }
      }
    }
    if (chatBox.ws) {
      chatBox.ws.send(JSON.stringify(msg));
    }
  }
  posUpdateOnline_mouseMove() {
    let msg = {
      code: 302,
      message: {
        token: localStorage.getItem("token"),
        data:{
          rotation:this.node.eulerAngles
        }
      }
    }
    if (chatBox.ws) {
      chatBox.ws.send(JSON.stringify(msg));
    }
  }

  onKeyDown(event: EventKeyboard) {
    console.log(event);
    let code = event.keyCode;
    console.log(code);
    let pos = this.node.getPosition();
    // this.posUpdateOnline()
    switch (code) {
      case 87:
        //按下w 向上移动
        player.keydown_w = true;
        this.posUpdateOnline_keyDown(87)
        break;
      case 83:
        //按下s 向上移动
        player.keydown_s = true;
        this.posUpdateOnline_keyDown(83)
        break;
      case 65:
        //按下a 向左移动
        player.keydown_a = true;
        this.posUpdateOnline_keyDown(65)
        break;
      case 68:
        //按下d 向右移动
        player.keydown_d = true;
        this.posUpdateOnline_keyDown(68)
        break;
      default:
        break;
    }
  }
  onKeyUp(event: EventKeyboard) {
    let code = event.keyCode;
    // this.posUpdateOnline()
    switch (code) {
      case 87:
        player.keydown_w = false;
        this.posUpdateOnline_keyUp(87)
        break;
      case 83:
        player.keydown_s = false;
        this.posUpdateOnline_keyUp(83)
        break;
      case 65:
        player.keydown_a = false;
        this.posUpdateOnline_keyUp(65)
        break;
      case 68:
        player.keydown_d = false;
        this.posUpdateOnline_keyUp(68)
        break;
      default:
        break;
    }
  }

  getDirection(x: number, y: number, z: number) {
    let ret = new Vec3(x, y, z);
    math.Vec3.transformQuat(ret, ret, this.node.getRotation());
    return ret;
  }

  update(deltaTime: number) {
    // [4]
    // console.log(deltaTime);
    if (player.keydown_w) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(0, 0, 1), deltaTime * 5);
      this.node.setPosition(pos);
    }
    if (player.keydown_s) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(0, 0, -1), deltaTime * 5);
      this.node.setPosition(pos);
    }
    if (player.keydown_a) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(1, 0, 0), deltaTime * 5);
      this.node.setPosition(pos);
    }
    if (player.keydown_d) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(-1, 0, 0), deltaTime * 5);
      this.node.setPosition(pos);
    }
  }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
